package com.guigu.youxuan.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.GoodsCategoryMapper;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.GoodsCategory;
import com.guigu.youxuan.pojo.GoodsInfo;
import com.guigu.youxuan.service.IGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements IGoodsCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public List<GoodsCategory> GoodsCategoryNameList() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("level",1);
        List goodsCategoryNameList=this.list(wrapper);
        return goodsCategoryNameList;
    }

    @Override
    public List<Integer> queryGoodsTypeLevel() {
        List<Integer> list = this.goodsCategoryMapper.queryGoodsTypeLevel();
        return list;
    }

    @Override
    public PageUtils<GoodsCategory> getGoodsCategoryList(GoodsCategory goodsCategory, int currentPage, int pageSize) {
        QueryWrapper<GoodsCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (goodsCategory != null) {
           if(!StringUtils.isEmpty(goodsCategory.getCategoryName()))
               wrapper.like("category_name",goodsCategory.getCategoryName());
           if(goodsCategory.getLevel() != null )
               wrapper.eq("level",goodsCategory.getLevel());
        }
        PageHelper.startPage(currentPage,pageSize);
        PageHelper.orderBy("id desc");

        List<GoodsCategory> list = this.list(wrapper);
        PageInfo<GoodsCategory> pageInfo = new PageInfo<>(list);
        PageUtils<GoodsCategory> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    @Override
    public void switchStatus(GoodsCategory goodsCategory) {
        if(goodsCategory != null)
            this.updateById(goodsCategory);
    }

    @Override
    public void addGoodsType(GoodsCategory goodsCategory) {
        if(goodsCategory.getParentId() != null) {
            GoodsCategory byId = this.getById(goodsCategory.getParentId());
            goodsCategory.setLevel(byId.getLevel()+1);
        } else {
            goodsCategory.setParentId((long)0);
            goodsCategory.setLevel((long)1);
        }
        this.save(goodsCategory);
    }

    @Override
    public GoodsCategory queryGoodsTypeById(Long goodsTypeId) {
        GoodsCategory goodsCategory = this.getById(goodsTypeId);
        return goodsCategory;
    }

    @Override
    public List<GoodsCategory> queryGoodsCategoryList() {
        QueryWrapper<GoodsCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        List<GoodsCategory> list = this.list(wrapper);
        return list;
    }

    @Override
    public void updateGoodsCategory(GoodsCategory goodsCategory) {
        if(goodsCategory.getParentId() != null) {
            GoodsCategory byId = this.getById(goodsCategory.getParentId());
            goodsCategory.setLevel(byId.getLevel()+1);
        }else {
            goodsCategory.setLevel((long)1);
            goodsCategory.setParentId((long)0);
        }
        this.updateById(goodsCategory);
    }

    @Override
    public void delGoodsCategory(GoodsCategory goodsCategory) {
        if(goodsCategory != null)
            goodsCategory.setIsDeleted((byte)1);
        this.updateById(goodsCategory);
    }

    @Override
    public void delsGoodsCategory(List<GoodsCategory> goodsCategoryList) {
        for(GoodsCategory goodsCategory : goodsCategoryList) {
            goodsCategory.setIsDeleted((byte)1);
            this.updateById(goodsCategory);
        }
    }
}
